//index.js
const app = getApp()

Page({
    data: {
        signNum: 0,
        signScore: 0,
        signed: false,
        showday3: false,
        showday7: false,
        days: [{
                day: "日",
                signed: false
            },
            {
                day: "一",
                signed: false
            },
            {
                day: "二",
                signed: false
            },
            {
                day: "三",
                signed: false
            },
            {
                day: "四",
                signed: false
            },
            {
                day: "五",
                signed: false
            },
            {
                day: "六",
                signed: false
            },
        ]
    },

    onLoad: function () {
        var today = new Date()
        today = today.getDay()
        var days = this.data.days

        // 判断签到按钮是否激活
        if (days[today].signed) {
            var signed = true
        }
        // 判断连续签到提示是否激活
        if (days[0].signed && days[1].signed && today == 2) {
            var showday3 = true
        }
        if (days[0].signed && days[1].signed && days[2].signed && days[3].signed && days[4].signed && days[5].signed && today == 6) {
            var showday7 = true
        }
        this.setData({
            signed: signed,
            showday3: showday3,
            showday7: showday7
        })
    },

    // 监听签到事件
    onSign: function () {

        if (!this.data.signed) {

            var that = this
            var today = new Date()
            today = today.getDay()
            var days = that.data.days
            days[today].signed = true //当日状态变更为“完成签到”

            that.setData({
                signNum: ++that.data.signNum,
                signScore: ++that.data.signScore,
                signed: true,
                days: days
            })

            if (days[0].signed && days[1].signed && today == 2) {
                that.setData({
                    signScore: that.data.signScore + 2,
                    showday3: false
                })
            }
            if (days[0].signed && days[1].signed && days[2].signed && days[3].signed && days[4].signed && days[5].signed && today == 6) {
                that.setData({
                    signScore: that.data.signScore + 6,
                    showday7: false
                })
            }
        }
    }
})